<template>
  <div class="login-container">
    <div class="login-center">
      <img src="../../assets/centerBg21.png" class="rightImg" alt="">
      <div class="center-left">
        <div class="center-left-head" style="">
          <img src="@/assets/logo.png" width="40%" alt="">
        </div>
        <div style=" width=100%;height:75%">
          <img
            src="@/assets/left.png"
            style="padding-left: 50px; padding-top: 30px"
            width="100%"
            height="auto"
            alt=""
          >
        </div>

        <!-- <div style="height:20%"></div> -->
      </div>
      <div class="center-right">
        <div class="form">
          <el-form
            ref="loginForm"
            :model="loginForm"
            class="login-form"
            autocomplete="on"
            label-position="center"
          >
            <div class="title-container">
              <h3 class="title">登录账号</h3>
            </div>

            <el-form-item prop="username">
              <el-input
                ref="username"
                v-model="loginForm.username"
                class="userInput"
                prefix-icon="el-icon-user-solid"
                placeholder="请输入用户名"
                name="username"
                type="text"
                tabindex="1"
                autocomplete="on"
              />
            </el-form-item>

            <el-tooltip
              v-model="capsTooltip"
              content="Caps lock is On"
              placement="right"
              manual
            >
              <el-form-item prop="password" style="margin-bottom: 0px">
                <el-input
                  :key="passwordType"
                  ref="password"
                  v-model="loginForm.password"
                  prefix-icon="el-icon-lock"
                  :type="passwordType"
                  placeholder="请输入密码"
                  name="password"
                  tabindex="2"
                  autocomplete="on"
                  @keyup.native="checkCapslock"
                  @blur="capsTooltip = false"
                  @keyup.enter.native="handleLogin"
                />
              </el-form-item>
            </el-tooltip>
            <div class="notice">
              <el-checkbox
                v-model="isLogin"
                label=""
                :disabled="disabled"
                class="notice_checkbox"
              />
              <div class="notice_text" @click="beforeLogin">
                我已阅读并同意须知内容
              </div>
            </div>
            <div style="width: 100%; text-align: center; margin-top: 40px">
              <el-button
                :loading="loading"
                style="width: 60%"
                @click.native.prevent="handleLogin"
              >登录</el-button>
            </div>
          </el-form>
        </div>
      </div>
    </div>
    <el-dialog title="保密协议" :visible.sync="showDialog">
      <div class="confirmText" v-html="html" />
      <div slot="footer" class="dialog-footer">
        <el-button @click="showDialog = false">取 消</el-button>
        <el-button type="primary" :disabled="disBtn" style="width: 100px" @click="confirmLogin">
          <span v-show="show">同 意</span>
          <span v-show="!show">{{ count }}</span>
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { validUsername } from '@/utils/validate'
import SocialSign from './components/SocialSignin'
import { getWordList } from '@/api/folder'

export default {
  name: 'Login',
  components: { SocialSign },
  data() {
    const validateUsername = (rule, value, callback) => {
      // if (!validUsername(value)) {
      //   callback(new Error('Please enter the correct user name'))
      // } else {
      callback()
      // }
    }
    const validatePassword = (rule, value, callback) => {
      if (value.length < 5) {
        callback(new Error('The password can not be less than 5 digits'))
      } else {
        callback()
      }
    }
    return {
      loginForm: {
        username: '',
        password: ''
      },
      // loginRules: {
      //   username: [
      //     { required: true, trigger: "blur", validator: validateUsername },
      //   ],
      //   password: [
      //     { required: true, trigger: "blur", validator: validatePassword },
      //   ],
      // },
      passwordType: 'password',
      capsTooltip: false,
      loading: false,
      showDialog: false,
      redirect: undefined,
      disabled: true,
      isLogin: false,
      disBtn: true,
      timer: '',
      count: 0,
      otherQuery: {},
      show: false,
      html: ''
    }
  },
  watch: {
    $route: {
      handler: function(route) {
        const query = route.query
        if (query) {
          this.redirect = query.redirect
          this.otherQuery = this.getOtherQuery(query)
        }
      },
      immediate: true
    },
    isLogin: {
      handler: function(flag) {
        if (!flag) {
          this.disabled = true
        }
      }
    }
  },
  created() {
    // window.addEventListener('storage', this.afterQRScan)
    // i = 0;
    // i++;
    // if (i == 1) {
    //   location.reload();
    // }
  },
  mounted() {
    if (this.loginForm.username === '') {
      this.$refs.username.focus()
    } else if (this.loginForm.password === '') {
      this.$refs.password.focus()
    }
  },
  destroyed() {
    // window.removeEventListener('storage', this.afterQRScan)
  },
  methods: {
    confirmLogin() {
      this.isLogin = true;
      (this.showDialog = false), (this.disabled = false)
    },
    beforeLogin() {
      this.showDialog = true
      new Promise((resolve, reject) => {
        getWordList().then((response) => {
          this.html = response.data[0].html
          this.count = 5
          this.timer = setInterval(() => {
            if (this.count > 0 && this.count < 6) {
              this.count--
            } else {
              this.disBtn = false
              this.show = true
              clearInterval(this.timer)
              this.timer = null
            }
          }, 1000)
        })
      })
    },
    checkCapslock(e) {
      const { key } = e
      this.capsTooltip = key && key.length === 1 && key >= 'A' && key <= 'Z'
    },
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    handleLogin() {
      if (this.loginForm.username == '') {
        this.$message({
          message: '请填写用户名!',
          type: 'error'
        })
        return
      }
      if (this.loginForm.password == '') {
        this.$message({
          message: '请填写密码!',
          type: 'error'
        })
        return
      }
      if (this.isLogin == false) {
        this.$message({
          message: '登录前请先同意须知!',
          type: 'error'
        })
        return
      }
      this.$refs.loginForm.validate((valid) => {
        if (valid) {
          this.loading = true
          this.$store
            .dispatch('user/login', this.loginForm)
            .then((response) => {
              this.$router.push('/')
              this.loading = false
              this.$message({
                message: '登录成功',
                type: 'success'
              })
            })
            .catch(() => {
              // console.log("我catch了");
              this.loading = false
            })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    getOtherQuery(query) {
      return Object.keys(query).reduce((acc, cur) => {
        if (cur !== 'redirect') {
        }
        return acc
      }, {})
    }
  }
}
</script>

<style>
.login-container {
  width: 100%;
  height: 100%;
  background-image: url("../../assets/loginBg.png");
  background-size: 100% 100%;
  background-repeat: no-repeat;
  z-index: 0;
  padding: 5% 10% 5% 10%;
  display: flex;
  align-items: center;
}

.login-container .login-center {
  width: 960px;
  height: 450px;
  margin: 0 auto;
  position: relative;
  z-index: 1;
  /* height: 100%; */
  border-radius: 5px;
  display: flex;
  flex-direction: row;
  /* border:red 1px solid; */
  background: white;
}
.login-container .login-center .rightImg {
  width: auto;
  right: 0;
  top: 0;
  height: 70%;
  position: absolute;
}
.login-container .center-left {
  width: 43%;
  /* border: 1px solid red; */
  height: 100%;
}
.login-container .center-right {
  width: 57%;
  /* border: 1px solid red; */
  height: 100%;
  padding: 7% 10% 7% 10%;
  /* padding: 10%; */
  z-index: 3;
}
.login-container .center-left-head {
  width: 100%;
  height: auto;
  /* border: 1px solid red; */
  /* align-items: center; */
  padding: 30px 0px 0px 50px;
}

.login-container .center-right .form {
  /* border: 1px solid red; */
  width: 100%;
  height: 100%;
}
.login-container .center-right .form .login-form {
  width: 100%;
  height: 100%;
  /* padding: 10%; */
}
.login-container .title-container {
  text-align: center;
  letter-spacing: 1px;
  margin-bottom: 60px;
  color: #281b6d;
}
.login-container .center-right .form .el-button {
  background: #281b6d;
  border: #281b6d;
  color: white;
  font-size: 14px;
  letter-spacing: 3px;
  font-weight: bold;
}
.login-container .center-right .form .el-button:hover {
  background: #281b6d;
  opacity: 0.7;
  border: #281b6d;
  color: white;
  font-size: 14px;
  letter-spacing: 3px;
  font-weight: bold;
}
.login-container .center-right .form .el-input__inner {
  border: none;
  border-bottom-width: 2px;
  border-bottom-style: ridge;
  border-radius: 0px;
  padding-bottom: 2px;
  background-color: transparent;
}
.center-right .form .el-input {
  margin-bottom: 10px;
}
.login-container .notice {
  margin: 0;
  display: flex;
  flex-direction: row;
  justify-content: baseline;
  margin-top: 10px;
}
.login-container .notice .notice_checkbox {
  width: 20px;
  height: 20px;
  font-size: 16px;
}
.login-container .notice .notice_text {
  font-size: 15px;
  color: rgb(85, 84, 84);
  padding-left: 3px;
}
.login-container .confirmText {
  height: 300px;
  overflow: scroll;
  overflow-x: hidden;
}
.login-container .el-dialog__footer {
  padding-top: 0px;
}
</style>
